//package com.ant.storage.dataSource;
//
//
//
//import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
//import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionTemplate;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.transaction.PlatformTransactionManager;
//
//import javax.sql.DataSource;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author hzd
// * @version 1.0
// * @date 2023/12/26 11:31
// */
//
//@Slf4j
//@Configuration(basePackages = MyBatisConfig.BASE_PACKAGE, sqlSessionTemplateRef = "sqlSessionTemplate")
//public class DynamicDataSourceRegister {
//
//    //mapper模式下的接口层
//    static final String BASE_PACKAGE = "cn.zhang.mapper";
//    public static final String CLASSPATH_MAPPING_XML = "classpath*:mapper/*.xml";
//
//    /**
//     * 配置数据源
//     *
//     * @return
//     */
//    @Bean(name = "db1")
//    @Primary
//    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.first")
//    public DataSource db1() {
//        return DruidDataSourceBuilder.create().build();
//    }
//
//    /**
//     * 配置数据源
//     *
//     * @return
//     */
//    @Bean(name = "db2")
//    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.second")
//    public DataSource db2() {
//        return DruidDataSourceBuilder.create().build();
//    }
//
//    @Bean(name = "dynamicDataSource")
//    public DataSource dynamicDataSource() {
//        DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
//        Map<Object, Object> dataSourceMap = new HashMap<Object, Object>(2);
//        dataSourceMap.put(DataSourceKey.Master.getName(), db1());
//        dataSourceMap.put(DataSourceKey.SLAVE.getName(), db2());
//        dynamicRoutingDataSource.setDefaultTargetDataSource(dcs());
//        dynamicRoutingDataSource.setTargetDataSources(dataSourceMap);
//        DynamicDataSourceContextHolder.dataSourceKeys.addAll(dataSourceMap.keySet());
//        return dynamicRoutingDataSource;
//    }
//
//    @Bean
//    public SqlSessionFactory sqlSessionFactory() throws Exception {
//        //MybatisPlus使用的是MybatisSqlSessionFactory
//        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
//        sqlSessionFactoryBean.setDataSource(dynamicDataSource());
//        //此处设置为了解决找不到mapper文件的问题
//        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(CLASSPATH_MAPPING_XML));
//        return sqlSessionFactoryBean.getObject();
//    }
//
//    @Bean
//    public SqlSessionTemplate sqlSessionTemplate() throws Exception {
//        return new SqlSessionTemplate(sqlSessionFactory());
//    }
//
//    /**
//     * 事务
//     *
//     * @return
//     */
//    @Bean
//    public PlatformTransactionManager transactionManager() {
//        return new DataSourceTransactionManager(dynamicDataSource());
//    }